package org.joshsacks.fishies.ui.gl;

import static javax.microedition.khronos.opengles.GL10.GL_TEXTURE_ENV;
import static javax.microedition.khronos.opengles.GL10.GL_TEXTURE_ENV_MODE;

import java.util.HashMap;
import java.util.Map;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Rect;
import android.opengl.GLUtils;

public class BitmapRenderedText implements RenderedText{
  private int height;
  private int width;
  private int texture;
  
  private final float[] textureCoor;

  public BitmapRenderedText(GL10 gl, String text, int fontSize) { 
    Paint textPaint = new Paint();
    textPaint.setTextSize(fontSize);
    textPaint.setAntiAlias(true);
    textPaint.setARGB(0xFF, 0xff, 0xff, 0xFF);
    textPaint.setTextAlign(Align.LEFT);
    Rect bounds = new Rect();
    textPaint.getTextBounds(text, 0, text.length(), bounds);
    width  = bounds.width() + 1;
    height = bounds.height();
    int width2 = 2;
    for (; width2 < width; width2 *= 2);
    int height2 = 2;
    for (; height2 < height; height2 *= 2);
    
    Bitmap bitmap = Bitmap.createBitmap(
            width2, height2, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    bitmap.eraseColor(0x00000000);

    // draw the text centered
    canvas.drawText(text, 0, height, textPaint);    
    System.err.println("FONT INIT CALLED");
    renderTexture(gl, bitmap);
    float wFraction = width / (float) width2;
    float hFraction = height / (float) height2;
    
    textureCoor = new float[] {
      0, hFraction,
      0, 0,
      wFraction, 0,
      wFraction, hFraction};
  }

  private void renderTexture(GL10 gl, Bitmap bitmap) {
    int[] textureArray = new int[1];
    //Generate one texture pointer...
    gl.glGenTextures(1, textureArray, 0);
    texture = textureArray[0];
    //...and bind it to our array
    gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);

    //Create Nearest Filtered Texture
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

    //Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);

    //Use the Android GLUtils to specify a two-dimensional texture image from our bitmap
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
  }
  
  public float draw(GL10 gl, float x, float y) {
//    gl.glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL10.GL_DECAL);  
    gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);
  
    gl.glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL10.GL_MODULATE);  
    SquareRenderer square = new SquareRenderer(gl);
    square.addRectangle(x, y, width, height, textureCoor);
    square.draw();
    return width;
  }

  public float getWidth() {
    return width;
  }

  public float getHeight() {
    return height;
  }

  public void discard(GL10 gl) {
    int[] textureArray = {texture};
    
    gl.glDeleteTextures(1, textureArray, 0);
  }  
}
